package scct.yzx.dao;

import scct.yzx.model.User;
import scct.yzx.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

// 封装了对user表的数据库操作
public class UserDao {
    public User login(String name, String pass) {
        User user = null;
//    获取数据库连接对象
        Connection conn = DBUtils.getConnection();
        String sql = "select*from sm_user where username=? and password=?";
//    预编译查询对象
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, pass);
//        查询
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String createtime = rs.getString("createtime");
                user = new User(id, username, password, createtime);
            }
            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return user;
    }

    /**
     * @param name 用户名
     * @param pass 密码
     * @return 用户对象
     */
    public boolean Sign(String name, String pass,String createtime) {
        int result = 0;
        Connection conn = DBUtils.getConnection();
        String sql = "insert into sm_user values(null,?,?,?)";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, pass);
            pstmt.setString(3, createtime);
            result = pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result == 1;
    }
    public boolean updateUser(int id,String password,String createtime){
        int count = 0;
        Connection conn = DBUtils.getConnection();
        String update_sql = "update sm_user set createtime=?,password=? where id = ?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(update_sql);
            pstmt.setString(1,createtime);
            pstmt.setString(2,password);
            pstmt.setInt(3,id);
            count = pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count > 0;
    }



    /**
     * @param name 用户名
     * @param pass 密码
     * @return 用户对象
     */

    // 添加用户
    public boolean addUser(String name, String pass, String createtime) {
        int result = 0;
        Connection conn = DBUtils.getConnection();
        String sql = "insert into sm_user values(null,?,?,?)";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, pass);
            pstmt.setString(3, createtime);
            result = pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result == 1;

    }



    public boolean isDupname(String name){
        int count = 0;// 查到的用户数量
        Connection conn = DBUtils.getConnection();
        String sql = "select*from sm_user where username=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,name);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()){
                count =1;
            }
//            DBUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return count>0;

    }

    public List<User> getAllUser() {
        Connection conn = DBUtils.getConnection();
        List<User> users = new ArrayList<>();
        String sql = "select * from sm_user";
        try {
            PreparedStatement pstme = conn.prepareStatement(sql);
            ResultSet rs = pstme.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String createtime = rs.getString("createtime");
                users.add(new User(id, username, password, createtime));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return users;
    }

    public User getUserById(int id){
        User user = null;
        Connection conn = DBUtils.getConnection();
        String sql = "select*from sm_user where id=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);

            ResultSet rs = pstmt.executeQuery();
            if (rs.next()){
                String username = rs.getString("username");
                String password = rs.getString("password");
                String createtime = rs.getString("createtime");
                user = new User(id,username,password,createtime);

            }
            DBUtils.closeConnection(conn);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return user;
    }

    public boolean deleteUser(int id) {
        int num = 0;
        Connection conn = DBUtils.getConnection();
        String sqlStr = "delete from sm_user where id = ?";
        try{
            PreparedStatement pstmt = conn.prepareStatement(sqlStr);
            pstmt.setInt(1,id);
            num = pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return num > 0;
    }
    //查询按名称
    public ArrayList<User> getUserByName(String name) {
        ArrayList<User> users = new ArrayList<>();
        Connection conn = DBUtils.getConnection();
        String sql = "select * from sm_user where username like '%" + name + "%'";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String createtime = rs.getString("createtime");
                users.add(new User(id, username,password,createtime));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return users;
    }

}
